package edu.sctu.xuping.month06.aboutinputandoutput;


import java.io.*;

/**
 * Created by user02
 */
public class IO {
    private static final String pathone = "D://12";
    private static final String pathtwo = pathone + "//13.txt";

    /**
     * 在指定路径下创建一个文件夹
     */
    public String createFolder() {
        File file = new File(pathone);
        if (!file.exists()) {
            file.mkdir();
        }
        return file.getName();
    }

    /**
     * 在指定文件夹下创建一个文件
     *
     * @throws IOException
     */
    public String createFile() throws IOException {
        File file = new File(pathtwo);
        if (!file.exists()) {
            file.createNewFile();
        }
        return file.getPath();
    }

    public void deleteFile() {
        File file = new File(pathtwo);

        file.delete();
    }

    /**
     * 文件的复制(读取字符的方式)
     *
     * @param path1
     * @param path2
     * @throws IOException
     */
    public void fileReaderUtil(String path1, String path2) throws IOException {
        FileReader filereader = new FileReader(new File(path1));
        FileWriter filewriter = new FileWriter(new File(path2));
        char[] buffer = new char[1024];
        int count = 0;
        while ((count = filereader.read(buffer)) != -1) {
            filewriter.write(buffer, 0, count);
            System.out.println(buffer);
        }
        filereader.close();
        filewriter.close();
    }

    /**
     * 文件的复制（读取字节的方式）
     *
     * @param path1
     * @param path2
     * @throws IOException
     */

    public void fileInputStreamUtil(String path1, String path2) throws IOException {
        int count = 0;
        int number = 0;
        byte[] buffer = new byte[1024];
        FileInputStream inputstream = new FileInputStream(new File(path1));
        FileOutputStream outputstream = new FileOutputStream(new File(path2));
        while ((number = inputstream.read(buffer)) != -1) {
            count += number;
            outputstream.write(buffer, 0, count);
            System.out.println(buffer);
        }
        inputstream.close();
        outputstream.close();
    }

}